package com.zhuangjie.coupon.pojo;

import lombok.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 相同优惠券具有相同的key
 *
 * @author zhuangjie
 * @date 2024/03/04
 */
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class CouponKey {
    protected Long batchId;

    protected BigDecimal expansionAmount;
    /**
     *  是否可叠加
     */
    protected Boolean isStackable;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        CouponKey couponKey = (CouponKey) o;

        // 如果不可以叠加，则直接返回false（不让其进行折叠）
        if (isStackable != null ? isStackable : false) {
            return false;
        }

        if (!Objects.equals(batchId, couponKey.batchId)) return false;
        if (!Objects.equals(isStackable, couponKey.isStackable)) return false;
        return Objects.equals(expansionAmount, couponKey.expansionAmount);
    }

    @Override
    public int hashCode() {
        int result = batchId != null ? batchId.hashCode() : 0;
        result = 31 * result + (expansionAmount != null ? expansionAmount.hashCode() : 0);
        result = 31 * result + (isStackable != null ? isStackable.hashCode() : 0);
        return result;
    }

    public static void main(String[] args) {
        CouponKey couponKey = new CouponKey();
        couponKey.setBatchId(1l);
        couponKey.setExpansionAmount(null);
        couponKey.setIsStackable(false);

        CouponKey couponKey2 = new CouponKey();
        couponKey2.setBatchId(1l);
        couponKey2.setExpansionAmount(null);
        couponKey2.setIsStackable(false);

        ArrayList<CouponKey> list = new ArrayList<>();
        list.add(couponKey);
        list.add(couponKey2);
        List<CouponKey> collect = list.stream().distinct().collect(Collectors.toList());
        System.out.println(list.size()+","+collect.size());


    }


}
